//
//  ShouzuoViewController.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ShouzuoViewController.h"
#import "aStageHeader.pch"
static NSInteger n = 0;
@interface ShouzuoViewController ()<UITableViewDataSource, UITableViewDelegate>
// tableView
@property (nonatomic, retain) UITableView *tableView;
// 请求数据
@property (nonatomic, retain) NSMutableArray *dataArr;
// 按钮下面的红线
@property (nonatomic, retain) UIView *redLine;
// tableView的header
@property (nonatomic, retain) UIView *header;
// 热门数据
@property (nonatomic, retain) NSArray *hotArr;
// header上面的Label imageView
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *imgView;
@property (nonatomic, assign) NSInteger template;
@property (nonatomic, assign) NSInteger last_id;
@end

@implementation ShouzuoViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}
// 视图即将出现
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = NO;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:234 / 255.0 green:86 / 255.0 blue:81 / 255.0 alpha:1.0];
    self.navigationItem.title = @"市集";
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sousuo@2x"] style:UIBarButtonItemStyleDone target:self action:@selector(rightAction)];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    // 设置头部上面的两个按钮
    UIButton *shouzuoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [shouzuoButton setTitle:@"手作集市" forState:UIControlStateNormal];
    [shouzuoButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    shouzuoButton.backgroundColor = [UIColor whiteColor];
    [shouzuoButton addTarget:self action:@selector(shouzuoButtonAction) forControlEvents:UIControlEventTouchUpInside];
    shouzuoButton.frame = CGRectMake(0, 64, WIDTH / 2, 50);
    [self.view addSubview:shouzuoButton];
    UIButton *cailiaoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cailiaoButton setTitle:@"材料商店" forState:UIControlStateNormal];
    [cailiaoButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    cailiaoButton.backgroundColor = [UIColor whiteColor];
    [cailiaoButton addTarget:self action:@selector(cailiaoButtonAction) forControlEvents:UIControlEventTouchUpInside];
    cailiaoButton.frame = CGRectMake(WIDTH / 2, 64, WIDTH / 2, 50);
    [self.view addSubview:cailiaoButton];
//    [self shouzuoButtonAction];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 114, WIDTH, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:line];
    self.header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH / 3)];
    _header.backgroundColor = [UIColor whiteColor];
    _tableView.tableHeaderView = _header;
    self.redLine = [[UIView alloc] init];
    _redLine.frame = CGRectMake(0, 0, WIDTH / 4, 2);
    _redLine.center = CGPointMake(WIDTH / 4, 113);
    _redLine.backgroundColor = [UIColor redColor];
    [self.view addSubview:_redLine];
    self.template = 1;
    self.last_id = 10;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadOldData)];
    [_tableView.mj_header beginRefreshing];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [_tableView.mj_footer beginRefreshing];
    [_tableView release];
}
- (void)rightAction
{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)loadNewData
{
    if (self.template == 1) {
        [AFNModel GETWithURL:[NSString stringWithFormat:@"http://m.shougongke.com/index.php?c=Shiji&a=topicList&last_id=%ld&page=product&vid=15", self.last_id] completion:^(id result) {
            NSArray *arrTemp = [TopicModel modelHandlerWithArray:result[@"data"]];
            for (TopicModel *m in arrTemp) {
                [self.dataArr addObject:m];
            }
            self.last_id += 10;
            [_tableView.mj_footer endRefreshing];
            [_tableView reloadData];
        }];
    } else {
        [AFNModel GETWithURL:[NSString stringWithFormat:@"http://m.shougongke.com/index.php?c=Shiji&a=topicList&last_id=%ld&vid=15", self.last_id] completion:^(id result) {
            NSArray *arrTemp = [TopicModel modelHandlerWithArray:result[@"data"]];
            for (TopicModel *m in arrTemp) {
                [self.dataArr addObject:m];
            }
            self.last_id += 10;
            [_tableView.mj_footer endRefreshing];
            [_tableView reloadData];
        }];
    }
}
- (void)loadOldData
{
    if (self.template == 1) {
        [self dataHandlerWithURL:@"http://m.shougongke.com/index.php?c=Shiji&a=product&vid=15"];
        self.last_id = 10;
        [_tableView.mj_header endRefreshing];
    } else {
        [self dataHandlerWithURL:@"http://m.shougongke.com/index.php?c=Shiji&vid=15"];
        [_tableView.mj_header endRefreshing];
        self.last_id = 10;
    }
}
- (void)btnActionWithButton:(UIButton *)button
{
    SJXQViewController *sjxqVC = [[SJXQViewController alloc] init];
    [self.navigationController pushViewController:sjxqVC animated:YES];
}
- (void)shouzuoButtonAction
{
    [self dataHandlerWithURL:@"http://m.shougongke.com/index.php?c=Shiji&a=product&vid=15"];
    _redLine.center = CGPointMake(WIDTH / 4, 113);
    self.template = 1;
    [_tableView.mj_header endRefreshing];
}
- (void)cailiaoButtonAction
{
    [self dataHandlerWithURL:@"http://m.shougongke.com/index.php?c=Shiji&vid=15"];
    _tableView.frame = CGRectMake(0, 50, WIDTH, HEIGHT);
    _redLine.center = CGPointMake(WIDTH * 3 / 4, 113);
    self.template = 2;
    self.last_id = 10;
}
- (void)dataHandlerWithURL:(NSString *)URL
{
    [AFNModel GETWithURL:URL completion:^(id result) {
        NSArray *arr = result[@"data"][@"topic"];
        self.dataArr = [TopicModel modelHandlerWithArray:arr];
        arr = result[@"data"][@"hot"];
        self.hotArr = [HotModel modelHandlerWithArray:arr];
        [self createButton];
        [_tableView reloadData];
    }];
    n++;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (void)createButton
{
    for (NSInteger i = 0; i < _hotArr.count; i++) {
        if (n == 1) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = 10000 + i;
            btn.frame = CGRectMake(20 + (WIDTH - 40) / 4 * i, 0, (WIDTH - 40) / 4, WIDTH / 3 - 10);
            [btn addTarget:self action:@selector(btnActionWithButton:) forControlEvents:UIControlEventTouchUpInside];
            self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, (WIDTH - 40) / 4 - 20, (WIDTH - 40) / 4 - 20)];
            [btn addSubview:_imgView];
            self.titleLabel = [[UILabel alloc] init];
            _titleLabel.frame = CGRectMake(10, (WIDTH - 40) / 4, (WIDTH - 40) / 4 - 20, WIDTH / 3 - (WIDTH - 40) / 4 - 20);
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            _titleLabel.font = [UIFont systemFontOfSize:14];
            [btn addSubview:_titleLabel];
            [self.header addSubview:btn];
        }
        HotModel *m = _hotArr[i];
        _titleLabel.text = m.name;
        [_imgView sd_setImageWithURL:[NSURL URLWithString:m.pic]];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH / 2;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicModel *m = _dataArr[indexPath.row];
    // 点击之后分为两种模板 用template判断
    // 模板1为collectionView 模板2为tableView
    if ([m.template isEqualToString:@"1"]) {
        FirstTemplateVC *firstTemplateVC = [[FirstTemplateVC alloc] init];
        firstTemplateVC.topicId = m.topic_id;
        [self.navigationController pushViewController:firstTemplateVC animated:YES];
    } else {
        SecondTemplateVC *secondTemplateVC = [[SecondTemplateVC alloc] init];
        secondTemplateVC.topicId = m.topic_id;
        [self.navigationController pushViewController:secondTemplateVC animated:YES];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    TopicModel *m = _dataArr[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.host_pic]];
    cell.title.text = m.topic_name;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
